home *** CD-ROM | disk | FTP | other *** search
/ Aminet 32 / Aminet 32 (1999)(Schatztruhe)[!][Aug 1999].iso / Aminet / comm / tcp / AmiVNC.lha / AmiVNC / vncauth.c < prev    next >
C/C++ Source or Header  |  1999-05-04  |  3KB  |  128 lines

  1. //  Copyright (C) 1997, 1998 Olivetti & Oracle Research Laboratory
  2. //
  3. //  This file is part of the VNC system.
  4. //
  5. //  The VNC system is free software; you can redistribute it and/or modify
  6. //  it under the terms of the GNU General Public License as published by
  7. //  the Free Software Foundation; either version 2 of the License, or
  8. //  (at your option) any later version.
  9. //
  10. //  This program is distributed in the hope that it will be useful,
  11. //  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. //  GNU General Public License for more details.
  14. //
  15. //  You should have received a copy of the GNU General Public License
  16. //  along with this program; if not, write to the Free Software
  17. //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,
  18. //  USA.
  19. //
  20. // If the source code for the VNC system is not available from the place 
  21. // whence you received this file, check http://www.orl.co.uk/vnc or contact
  22. // the authors on vnc@orl.co.uk for information on obtaining it.
  23.  
  24.  
  25. /*
  26.  *  Functions for VNC password management and authentication.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <time.h>
  34. #include <sys/types.h>
  35. #include <sys/stat.h>
  36. #include "vncauth.h"
  37. #include "d3des.h"
  38.  
  39. /*
  40.  *   We use a fixed key to store passwords, since we assume that our local
  41.  *   file system is secure but nonetheless don't want to store passwords
  42.  *   as plaintext.
  43.  */
  44.  
  45. unsigned char fixedkey[8] = {23,82,107,6,35,78,88,7};
  46.  
  47. /*
  48.  *   Encrypt a password and store it in a file.
  49.  */
  50. int
  51. vncEncryptPasswd(char *passwd, char *encryptedPasswd)
  52. {
  53.     int i;
  54.  
  55.     /* pad password with nulls */
  56.  
  57.     for (i = 0; i < MAXPWLEN; i++) {
  58.     if (i < strlen(passwd)) {
  59.         encryptedPasswd[i] = passwd[i];
  60.     } else {
  61.         encryptedPasswd[i] = 0;
  62.     }
  63.     }
  64.  
  65.     /* Do encryption in-place - this way we overwrite our copy of the plaintext
  66.        password */
  67.  
  68.     deskey(fixedkey, EN0);
  69.     des(encryptedPasswd, encryptedPasswd);
  70.  
  71.     return 8;
  72. }
  73.  
  74. /*
  75.  *   Decrypt a password.
  76.  */
  77. char *
  78. vncDecryptPasswd(char *intext, char *outtext)
  79. {
  80.  
  81.     deskey(fixedkey, DE1);
  82.     des(intext, outtext);
  83.  
  84.     outtext[MAXPWLEN] = 0;
  85.  
  86.     return (char *)outtext;
  87. }
  88.  
  89. /*
  90.  *   Generate a set of random bytes for use in challenge-response authentication.
  91.  */
  92. void
  93. vncRandomBytes(unsigned char *where) {
  94.   int i;
  95.   unsigned int seed = (unsigned int) time(0);
  96.  
  97.   srand(seed);
  98.   for (i=0; i < CHALLENGESIZE; i++) {
  99.     where[i] = (unsigned char)(rand() & 255);    
  100.   }
  101. }
  102.  
  103. /*
  104.  *   Encrypt some bytes in memory using a password.
  105.  */
  106. void
  107. vncEncryptBytes(unsigned char *where, const char *passwd)
  108. {
  109.     unsigned char key[8];
  110.     int i;
  111.  
  112.     /* key is simply password padded with nulls */
  113.  
  114.     for (i = 0; i < 8; i++) {
  115.     if (i < strlen(passwd)) {
  116.         key[i] = passwd[i];
  117.     } else {
  118.         key[i] = 0;
  119.     }
  120.     }
  121.  
  122.     deskey(key, EN0);
  123.  
  124.     for (i = 0; i < CHALLENGESIZE; i += 8) {
  125.     des(where+i, where+i);
  126.     }
  127. }
  128.